﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CNotebook
{
    public class CardDivisionManager:ConnectionManager
    {
        public DataTable GetDivision()
        {
            SqlCommand oCommand = new SqlCommand();
            DataSet ds = new DataSet();

            oCommand.Connection = this.Connection;
            oCommand.CommandText = "SELECT * FROM M_CARDDIVISION";

            SqlDataAdapter oAdapter = new SqlDataAdapter();
            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(ds);

            return ds.Tables[0];
        }

        public SqlException AddDivision(string _DIV_CODE, string _DIV_NAME)
        {
            SqlCommand oCommand = new SqlCommand();

            try
            {
                oCommand.Connection = this.Connection;
                oCommand.CommandText = "INSERT INTO M_CARDDIVISION (DivCode, DivName) VALUES (@DivCode, @DivName)";

                oCommand.Parameters.Add(new SqlParameter("@DivCode", _DIV_CODE));
                oCommand.Parameters.Add(new SqlParameter("@DivName", _DIV_NAME));


                oCommand.ExecuteNonQuery();

                return null;
            }
            catch (SqlException ex)
            {
                return ex;
            }
            finally
            {
                oCommand.Dispose();
            }
        }

        public SqlException UpdateDivision(DataTable dt)
        {
            SqlCommand oCommand = new SqlCommand();
            try
            {
                foreach (DataRow oRow in dt.Rows)
                {
                    if (oRow.RowState == DataRowState.Added)
                    {
                        oCommand = new SqlCommand();
                        oCommand.Connection = this.Connection;
                        oCommand.CommandText = "INSERT INTO M_CARDDIVISION (DivName, DocStatus) VALUES (@DivName, @DocStatus)";

                        oCommand.Parameters.Add(new SqlParameter("@DocStatus", oRow["DocStatus"].ToString()));
                        oCommand.Parameters.Add(new SqlParameter("@DivName", oRow["DivName"].ToString()));


                        oCommand.ExecuteNonQuery();
                    }

                    if (oRow.RowState == DataRowState.Modified)
                    {
                        oCommand = new SqlCommand();
                        oCommand.Connection = this.Connection;
                        oCommand.CommandText = "UPDATE M_CARDDIVISION SET DivName=@DivName, DocStatus=@DocStatus WHERE DivCode=@DivCode";

                        oCommand.Parameters.Add(new SqlParameter("@DivCode", oRow["DivCode"].ToString()));
                        oCommand.Parameters.Add(new SqlParameter("@DivName", oRow["DivName"].ToString()));
                        oCommand.Parameters.Add(new SqlParameter("@DocStatus", oRow["DocStatus"].ToString()));


                        oCommand.ExecuteNonQuery();
                    }
                }

                return null;
            }
            catch (SqlException ex)
            {
                return ex;
            }
            finally
            {
                oCommand.Dispose();
            }
        }

        
    }
}
